﻿using System;
using System.Collections.Generic;
using System.Text;
using Chaliy.DbPro.Generators.TextAuthoring.Configuration;
using System.Collections.ObjectModel;

namespace Chaliy.DbPro.Generators.TextAuthoring
{
    /// <summary>
    ///     Implmentation of the <see cref="T:Chaliy.DbPro.Generators.TextAuthoring.ITextAuthor"/>. Look <see cref="T:Chaliy.DbPro.Generators.TextAuthoring.ITextAuthor"/> for more details.
    /// </summary>
    /// <seealso cref="T:Chaliy.DbPro.Generators.TextAuthoring.ITextAuthor"/>
    /// <seealso cref="T:Chaliy.DbPro.Generators.TextAuthoring.Configuration.TextConfiguration"/>
    public class TextAuthor : ITextAuthor
	{
		#region Fields

		private IParagraphAuthor paragraphAuthor;

		#endregion

		#region Constructors

		public TextAuthor(IParagraphAuthor paragraphAuthor)
		{
			if (paragraphAuthor == null)
			{
				throw new ArgumentNullException("paragraphAuthor");
			}
			this.paragraphAuthor = paragraphAuthor;
		}

		#endregion

		#region Properties

		protected IParagraphAuthor ParagraphAuthor
		{
			get
			{
				return this.paragraphAuthor; 
			}
		}
		
		#endregion

		#region Methods

        /// <summary>
        ///     Generates the text with specified lengh.
        /// </summary>
        /// <param name="length">The lengh of the text to generate.</param>
        /// <returns>Generated text with specified length.</returns>
        public string Generate(int length)
		{
			int currentLengh = 0;
			
			StringBuilder textBuffer = new StringBuilder(length + 255);

			while (currentLengh < length)
			{
				textBuffer.Append(this.GenerateParagrapah());
				textBuffer.Append(Environment.NewLine);

				currentLengh = textBuffer.Length;
			}

			return textBuffer.ToString(0, length);
		}

		protected virtual string GenerateParagrapah()
		{
			return this.ParagraphAuthor.Generate();	
		}
				
		#endregion
	}
}
